

import BaseStorage from "./BaseStorage";
import { isNull } from "../tools/Define";


export default class LocalMap extends BaseStorage {


    protected value: Object;
    protected count: number = 0;
    protected loadValue() {
        let localValue = this.getStorage();
        if (!isNull(localValue)) {
            this.setValue(localValue)
            for (const key in localValue) {
                if (localValue.hasOwnProperty(key)) {
                    this.count++;

                }
            }
            return true
        } else {
            this.value = {}
            return false
        }

    }

    size() {
        return this.count;
    }

    get(key: any) {
        let data = this.value[key];
        if (isNull(data)) {
            data = this.initValue;
        }
        this.set(key, data)
        return data;
    }

    has(key: any) {
        return !isNull(this.value[key])
    }

    updateValue(key, value) {
        this.set(key, this.get(key) + value)
    }

    set(key: any, value) {
        if (!this.value[key]) {
            this.count++;
        }
        if (this.value[key] != value) {
            this.value[key] = value
            this.saveValue();
        }

    }

    remove(key) {
        if (this.value[key]) {
            this.count--;
            delete this.value[key]
            this.saveValue();
        }

    }
}
